<?php

namespace app\admin\controller;

use app\admin\model\WorkTemplateLog;
use app\common\controller\Backend;
use app\admin\model\AuthGroupAccess;
use think\Db;
/**
 * 我的工资管理
 *
 * @icon fa fa-circle-o
 */
class MySalary extends Backend
{

    /**
     * Salary模型对象
     * @var \app\admin\model\SalaryItem
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\SalaryItem();
        $AuthGroupAccessInfo = AuthGroupAccess::where('uid',$this->auth->id)->find();
        if(empty($AuthGroupAccessInfo))$this->error('登录错误');
        if(!in_array($AuthGroupAccessInfo['group_id'],[1,6,8,24])){ //权限见admin
            $this->model->where('b_salary_item.admin_id',$this->auth->id);
        }
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            if(!in_array($this->admin_group['id'],[1,6,8])){
                $list = $this->model
                    ->with(['admin','salary'])
                    ->where($where)
                    ->where('result',2)
                    ->order($sort, $order)
                    ->limit(1)
                    ->select();
                $result = array("total" => 1, "rows" => $list);
            }else{
                $list = $this->model
                    ->with(['admin','salary'])
                    ->where($where)
                    ->where('result',2)
                    ->order($sort, $order)
                    ->paginate($limit);
                $result = array("total" => $list->total(), "rows" => $list->items());
            }

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function gongzi($ids=null)
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['admin','salary','benchmark'])
                ->where($where)
                ->where('salary_id',$_SESSION['gongzi_admin'])
                ->group('admin_id')
                ->select();
            $data = [];
            foreach ($list as $v){
                $arr = [];
                $arr['admin'] = $v->admin;
                $contents = json_decode($v->content,1);
                foreach ($contents as $c){
                    $arr[$c['name']] = $c['money'];
                }
                $data[] = $arr;
            }
            $sort = str_replace('salary_item.', "", $sort);
            if($sort != 'id'){

                // 提取需要排序的字段作为关联数组
                foreach ($data as $key => &$val) {
                    $ages[$key] = $val[$sort];
                }
                unset($val); // 释放引用变量
                // 按年龄从小到大排序
                if($order == 'asc'){
                    array_multisort($ages, SORT_ASC, $data);
                }else{
                    array_multisort($ages, SORT_DESC, $data);
                }
            }
            $result_data = [];
            $num = 0;
            foreach ($data as $k=>$v){
                if($offset == 0){
                    $offset = -1;
                }
                if($k > $offset && $num <= $limit){
                    $result_data[] = $v;
                    $num++;
                }
            }
            $result = array("total" => count($list), "rows" => $result_data);

            return json($result);
        }
        $_SESSION['gongzi_admin'] = $ids;
        $salary = Db::table('b_salary')->where('id',$ids)->find();
        $tpl_id = $salary['template_id'];
        $tpl = Db::table('b_salary_calculation_template_item')->where('template_id',$tpl_id)->column('item_name');
        $tpl[] = '合计';

        $tpls = [];
        $one = Db::table('b_salary_item')->where('salary_id',$_SESSION['gongzi_admin'])->value('content');
        $one = $one?json_decode($one,1):[];
        foreach ($tpl as $t){
            $existence = false;
            foreach ($one as $o){
                if($o['name'] == $t){
                    $existence = true;
                }
            }
            if($existence){
                $tpls[] = $t;
            }
        }
        $this->view->assign('tpl',json_encode($tpls));

        $list = Db::table('b_salary_benchmark')->field('id,title')->where('count_type',2)->select();
        $item = Db::table('b_salary_benchmark_item')->field('id,salary_benchmark_id,name')->select();
        foreach ($list as &$value){
            $items = [];
            foreach ($item as $i){
                if($value['id'] == $i['salary_benchmark_id']){
                    $items[] = $i;
                }
            }
            $value['items'] = $items;
        }
        $this->assign('list',json_encode($list));
        return $this->view->fetch();
    }
    /**
     * 查看
     */
    public function detail($ids=null)
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        $row = $this->model->where(['id' => $ids])->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row->content = json_decode($row->content,true);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function statistics($ids=null){
        $this->relationSearch = true;
        $row = $this->model->where(['id' => $ids])->find();
        $salary_id = $row->salary_id;
        $salary = Db::table('b_salary')->where('id',$salary_id)->find();
        $start_time =  $salary['start_time'];
        $end_time =  $salary['end_time'];
        $admin_id = $row->admin_id;
        // 计算所有次数
        $datas = [];
        for ($i=1;$i<25;$i++){
            switch ($i) {
                case 1:  // 值白班
                    // 计算次数
                    $datas[$i] = calculate_count(1,$admin_id,$start_time,$end_time);
                    break;
                case 2:  // 值夜班
                    $datas[$i] = calculate_count(2,$admin_id,$start_time,$end_time);
                    break;
                case 3:  // 值夜宿班
                    $datas[$i] = calculate_count(3,$admin_id,$start_time,$end_time);
                    break;
                case 4:  // 社团费用
                    $datas[$i] = calculate_count(4,$admin_id,$start_time,$end_time);
                    break;
                case 5:  // 课业辅导费用
                    $datas[$i] = calculate_count(5,$admin_id,$start_time,$end_time);
                    break;
                case 6:  // 护导费用
                    $datas[$i] = calculate_count(6,$admin_id,$start_time,$end_time);
                    break;
                case 7:  // 代班主任费用
                    $datas[$i] = simple_count(7,$admin_id,$start_time,$end_time);
                    break;
                case 8:  // 被代班主任费用
                    $datas[$i] = simple_count(8,$admin_id,$start_time,$end_time);
                    break;
                case 11:  // 主科-课时费用
                    $datas[$i] = subject_calculate(11,$admin_id,$start_time,$end_time);
                    break;
                case 12:  // 主科-代课费用
                    $datas[$i] = subject_calculate(12,$admin_id,$start_time,$end_time);
                    break;
                case 13:  // 主科-被代课费用
                    $datas[$i] = subject_calculate(13,$admin_id,$start_time,$end_time);
                    break;
                case 14:  // 科任-课时费用
                    $datas[$i] = subject_calculate(14,$admin_id,$start_time,$end_time);
                    break;
                case 15:  // 科任-代课费用
                    $datas[$i] = subject_calculate(15,$admin_id,$start_time,$end_time);
                    break;
                case 16:  // 科任-被代课费用
                    $datas[$i] = subject_calculate(16,$admin_id,$start_time,$end_time);
                    break;
                case 17:  // 班队会-课时费用
                    $datas[$i] = subject_calculate(17,$admin_id,$start_time,$end_time);
                    break;
                case 18:  // 班队会-代课费用
                    $datas[$i] = subject_calculate(18,$admin_id,$start_time,$end_time);
                    break;
                case 19:  // 班队会-被代课费用
                    $datas[$i] = subject_calculate(19,$admin_id,$start_time,$end_time);
                    break;
                case 20:  // 社团-主
                    $datas[$i] = calculate_count(8,$admin_id,$start_time,$end_time);
                    break;
                case 21:  // 社团-助
                    $datas[$i] = calculate_count(9,$admin_id,$start_time,$end_time);
                    break;
                default:
                    $value = 0;
                    break;
            }
        }

        // 分餐
        $datas[50] =  WorkTemplateLog::where(['type'=>1,'status'=>5,'admin_id'=>$admin_id])->whereTime('select_time', 'between', [$start_time, $end_time])->count();
        $datas[51] =  WorkTemplateLog::where(['type'=>2,'status'=>5,'admin_id'=>$admin_id])->whereTime('select_time', 'between', [$start_time, $end_time])->count();

        $this->view->assign("datas", $datas);
        return $this->view->fetch();


    }
    
    /**
     * 预览
     */
    public function preview($ids=null)
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        $row = $this->model->where(['id' => $ids])->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $content = json_decode($row->content,true);
//        dump($row->content);exit;
        foreach ($content  as $k=>$v){
            if($v['item_type']==4){
                unset($content[$k]);
            }
        }
        $row->content= $content;
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


}
